/*
 * author: lujiandong
 * date:2008-4-13
 * description: test strtok_r
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void str()
{
    char * str;
    str = (char *)malloc(20);
    bzero(str,20);
    sprintf( str, "123%s456%s789%s","oo","oo","oo");
    printf("%s length=%d\n",str,strlen(str));
    int length = strlen(str);    

    char * tok=NULL;
    char *myStrBuf=NULL;
    int    idx = 0;
    printf("%d  while...\n",(int)(str));
    while (1) {
        if (!idx)
            tok = strtok_r(str,"oo",&myStrBuf);
        else
            tok = strtok_r(NULL,"oo",&myStrBuf);
        
        printf("\n\nidx = %d\n",idx);
        if (tok) {
            printf( "%s length = %d %d\n\n",tok,strlen(tok),(int)(tok));
            idx++;
        }
        else{
            printf("NULL\n\n");
            break;
            //exit(0);
        }
    }
    printf("%s\n",str);
    int i =0;
    for(;i<length;i++){    
        printf("%d ",(int)(str[i]));
    }
    printf("\n");
    if (myStrBuf){
        printf("%d\n",(int)(myStrBuf));
        //不要free myStrBuf
    }
}

int main()
{
    char * str;
    str = (char *)malloc(20);
    bzero(str,20);
    sprintf( str, "123%s456%s789%s","oo","oo","oo");

    printf("%s length=%d\n",str,strlen(str));

    int length = strlen(str);    
    char * tok=NULL;
    char *myStrBuf=NULL;
    int    idx = 0;

    while (1) {
        if (!idx)
            tok = strtok_r(str,"oo",&myStrBuf);
        else
            tok = strtok_r(NULL,"oo",&myStrBuf);
        
        printf("idx = %d\n",idx);
        if (tok) {
            printf( "%s length = %d %d\n\n",tok,strlen(tok),(int)(tok));
            idx++;
        }
        else{
            printf("NULL\n\n");
            break;
            //exit(0);
        }
    }

}
